package cn.com.lms.business.examRewrite.service;

import cn.com.lms.business.examRewrite.persistence.entity.ExerciseQuestionReWrite;
import cn.com.lms.business.examRewrite.persistence.entity.ExerciseReWrite;
import cn.com.lms.business.examRewrite.persistence.repository.ExerciseQuestionReWriteDao;
import com.bnzj.core.persistence.BaseService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ExerciseQuestionReWriteService extends BaseService<ExerciseQuestionReWriteDao,ExerciseQuestionReWrite,Long> {
    /**
     * 返回一个数据库没有数据的QuestionId
     * @param exerciseReWrite
     * @return
     */
    public List<ExerciseQuestionReWrite> getUniqueQuestionId(ExerciseReWrite exerciseReWrite) {
        List<ExerciseQuestionReWrite> exerciseQuestionReWrites = new ArrayList<>();
        //TODO 数据库的
        List<ExerciseQuestionReWrite> exerciseQuestionList1 = repository.findByExerciseId(exerciseReWrite.getId());
        //TODO 前端
        List<ExerciseQuestionReWrite> exerciseQuestionList = exerciseReWrite.getExerciseQuestionList();
        for (ExerciseQuestionReWrite questionReWrite : exerciseQuestionList) {
            Boolean result = false;
            for (ExerciseQuestionReWrite exerciseQuestionReWrite : exerciseQuestionList1) {
                if (exerciseQuestionReWrite.getQuestionId()==questionReWrite.getQuestionId()){
                    result = true;
                    break;
                }
            }
            if (!result){
                ExerciseQuestionReWrite exerciseQuestionReWrite1 = new ExerciseQuestionReWrite();
                exerciseQuestionReWrite1.setQuestionId(questionReWrite.getQuestionId());
                exerciseQuestionReWrites.add(exerciseQuestionReWrite1);
            }
        }
        return exerciseQuestionReWrites;
    }

    /**
     * 批量保存question数据
     * @param exerciseReWriteData
     */
    public void batchSave(ExerciseReWrite exerciseReWriteData) {
        List<ExerciseQuestionReWrite> exerciseQuestionList = exerciseReWriteData.getExerciseQuestionList();
        if (exerciseQuestionList!=null && exerciseQuestionList.size()>0){
            exerciseQuestionList.parallelStream().forEach(exerciseQuestionReWrite -> {
                exerciseQuestionReWrite.setCreator(exerciseReWriteData.getCreator());
                exerciseQuestionReWrite.setExerciseId(exerciseReWriteData.getId());
                exerciseQuestionReWrite.setIsDelete(false);
                exerciseQuestionReWrite.setCreateTime(new Date());
                exerciseQuestionReWrite.setCreatorName(exerciseReWriteData.getCreatorName());
                //xxx
                save(exerciseQuestionReWrite);
            });
        }
    }


    /**
     * 通过id(exerciseId)获取ExerciseQuestionReWrite
     * @param id
     * @return
     */
    public List<Long> getQuestionIdByExerciseId(Long id) {
        return repository.getQuestionIdByExerciseId(id);
    }

    public void deleteSByExerciseId(boolean result, List<Long> id) {
        repository.deleteSByExerciseId(result,id);
    }
}
